﻿using HZ.Public;
using System;
using System.IO;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.NetworkInformation;
using System.Reflection.Emit;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfAppDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        int Status = 0;
        double a, b, c;

        public MainWindow()
        {
            InitializeComponent();
            InitRuleProtocols();
            //OpenSetting();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Label1.Content = "Hello World";
        }

        private void Button3_Click(object sender, RoutedEventArgs e)
        {
            Window1 window1 = new Window1();
            //window1.ShowDialog();/*以模式方法打开*/
            window1.Show();/*以无模式方法打开*/
        }

        private void Button4_Click(object sender, RoutedEventArgs e)
        {
            Window2 window2 = new Window2();
            window2.Show();
        }

        private void Button5_Click(object sender, RoutedEventArgs e)
        {
            Window3 window3 = new Window3();
            window3.Show();
        }

        private void Button6_Click(object sender, RoutedEventArgs e)
        {
            Window4 window4 = new Window4();
            window4.Show();
        }

        private void Button2_Click(object sender, RoutedEventArgs e)
        {
            if (TextBox1.Text.Length != 0)
            {
                a = Convert.ToDouble(TextBox1.Text.ToString());
            }
            else { a = 0; }
            if (TextBox2.Text.Length != 0)
            {
                b = Convert.ToDouble(TextBox2.Text.ToString());
            }
            else { b = 0; }
            switch (Status) {
                case 1: c = a + b; break;
                case 2: c = a - b; break;
                case 3: c = a * b; break;
                case 4: c = a / b; break;
            }
            TextBox3.Text = c.ToString();
        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            if (RadioButton1.IsChecked == true && RadioButton2.IsChecked == false && RadioButton3.IsChecked == false && RadioButton4.IsChecked == false)
            {
                MessageBox.Show("选择加运算");
                Status = 1;
                Label2.Content = "+";
            }
            else if (RadioButton1.IsChecked == false && RadioButton2.IsChecked == true && RadioButton3.IsChecked == false && RadioButton4.IsChecked == false)
            {
                MessageBox.Show("选择减运算");
                Status = 2;
                Label2.Content = "-";
            }
            else if (RadioButton1.IsChecked == false && RadioButton2.IsChecked == false && RadioButton3.IsChecked == true && RadioButton4.IsChecked == false)
            {
                MessageBox.Show("选择乘运算");
                Status = 3;
                Label2.Content = "*";
            }
            else if (RadioButton1.IsChecked == false && RadioButton2.IsChecked == false && RadioButton3.IsChecked == false && RadioButton4.IsChecked == true)
            {
                MessageBox.Show("选择除运算");
                Status = 4;
                Label2.Content = "/";
            }
            else
            {
                Status = 0;
                Label2.Content = "运算符";
            }
        }

        private List<RuleProtoclClass> GetIsRun()
        {
            List<RuleProtoclClass> ret = new List<RuleProtoclClass>();
            if (RuleData.RuleList != null)
            {
                foreach (var item in RuleData.RuleList)
                {
                    if (item.IsRun)
                    {
                        ret.Add(item);
                    }
                }
            }
            return ret;
        }

        private string GetActDesc(int Act)
        {
            string ret = "";
            if (Act == 0)
            {
                ret = PublicClass.GetLanguage("删除");
            }
            else
            {
                ret = PublicClass.GetLanguage("备份");
            }
            return ret;
        }

        /// <summary>
        /// 初始化协议
        /// </summary>
        private void InitRuleProtocols()
        {
            if (File.Exists(PublicClass.InfoDir + "RuleProtocol.json"))
                RuleData.RuleList = PublicClass.ReadDeserializeObject<ObservableCollection<RuleProtoclClass>>(PublicClass.InfoDir + "RuleProtocol.json");

            if (RuleData.RuleList == null)
                RuleData.RuleList = new ObservableCollection<RuleProtoclClass>();
        }

        /// <summary>
        /// 主框架在启动时会主动以线程来调用该方法
        /// 如果需要循环，也在此函数中自行实现
        /// </summary>
        public void LoopExecution(PublicClass.ProtocolInfoHandler callback = null)
        {

            while (true)
            {
                List<RuleProtoclClass> list = GetIsRun();

                foreach (var item in list)
                {
                    if (item != null)
                    {
                        if (item.Type == 0) //按时间
                        {
                            DateTime exec = Convert.ToDateTime(item.NextExecTime);
                            if (DateTime.Compare(DateTime.Now, exec) > 0)
                            {
                                item.LastExecTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                                item.NextExecTime = item.GetNextExecTime(item.NextExecTime, item);
                                string strAlarm = string.Format(PublicClass.GetLanguage("系统于{0}自动进行数据{1}"), item.LastExecTime, GetActDesc(item.ActType));
                                if (callback != null)
                                    callback(strAlarm);
                                if (item.ActType == 0)
                                {
                                    item.RunAutoDelete(item.LastExecTime);
                                    MsgPumpClass.PublishMsg("DataQuery.RefreshTestDataList");
                                }
                                else
                                {
                                    item.RunAutoBackup();

                                }
                            }
                        }
                    }
                }

                Thread.Sleep(1000);
                if (PublicClass.bQuit)
                    break;
            }
        }
        /// <summary>
        /// 注框架会主动调用这个界面来打开一个UI，该UI一般是进行一些配置的，如果没有配置，也可以不实现
        /// </summary>
        public void OpenSetting()
        {
            UserControl4 uc = new UserControl4();
            PublicClass.ShowUCWin_CanResize(uc, PublicClass.GetLanguage("数据策略维护"), 800, 600, uc.DoBeforeCloseWin, null, null, true);
        }

    }

}
